package com.seio.edc.controller.system;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nova.edc.service.system.AreaPointService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.GatewayImage;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.system.CollectAreaService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.seio.edc.common.Constants;
import com.seio.edc.controller.core.BaseContoller;

@Controller
@RequestMapping(value = "/gatewayImage")
public class GatewayImageContoller extends BaseContoller {
	private static Logger log = Logger.getLogger(GatewayImageContoller.class);

	@Autowired
	private com.nova.edc.service.system.GatewayImageService novaGatewayImageService;

	@Autowired
	private CollectAreaService novaCollectAreaService;

	@Autowired
	private AreaPointService novaareaPointService;

	private final int PAGE_SIZE = 10;

	/**
	 * 用户表页面初始化
	 */
	@RequestMapping(value = "gatewayImageInit.shtml")
	public ModelAndView gatewayImageList(HttpServletRequest request,
			HttpSession session, Integer pageIndex, GatewayImage gatewayImage,
			ModelMap modelMap) {
		Login login = (Login) session.getAttribute("sessionLogin");
		gatewayImage.setAddby(login.getUsername());
		gatewayImage.setScrapped(ModelConstant.SCRAPPED_UNDELETE);
		// 当前页数
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());

		PageList<GatewayImage> pageList = novaGatewayImageService
				.findPageGatewayImage(gatewayImage, page, "AddDate", "desc");
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		// 数据列表
		modelMap.put("list", pageList.getDatas());

		return new ModelAndView("system/gatewayImageList", modelMap);
	}

	/**
	 * 添加初始化
	 */
	@RequestMapping(value = "editGatewayImageInit.shtml")
	public ModelAndView addcollectAreaInit(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer gatewayImageId) {

		// 取出所有网关
		List<String> gatewayIDlist = novaareaPointService.getAllGateway();
		modelMap.put("gatewayIDlist", gatewayIDlist);
		if (gatewayImageId != null && gatewayImageId != 0) {
			GatewayImage gatewayImage = novaGatewayImageService
					.getById(gatewayImageId);
			modelMap.put("gatewayImage", gatewayImage);
		}

		return new ModelAndView("system/gatewayImageEdit", modelMap);
	}

	/**
	 * 添加网关图片
	 */
	@RequestMapping(value = "saveGatewayImage.shtml")
	public ModelAndView saveGatewayImage(GatewayImage gatewayImage,
			BindingResult result, HttpServletRequest request,
			HttpSession session, ModelMap modelMap) {
		Login login = (Login) session.getAttribute("sessionLogin");
		// 文件上传--------开始
		try {
			// 设置上下方文
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			// 检查form是否有enctype="multipart/form-data"
			if (multipartResolver.isMultipart(request)) {
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				Iterator<String> iter = multiRequest.getFileNames();
				while (iter.hasNext()) {
					// 由CommonsMultipartFile继承而来,拥有上面的方法.
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						String path = Constants.UploadImagesPath
								+ file.getOriginalFilename();
						// 保存文件路径
						gatewayImage.setImageurl(path);
						File localFile = new File(path);
						file.transferTo(localFile);
					}
				}
			}
			modelMap.put("msg", "文件上传成功！");
		} catch (Exception e) {
			log.error("文件上传失败：" + e.getStackTrace());
			modelMap.put("msg", "文件上传失败！");
		}
		// 文件上传--------结束
		if (gatewayImage.getId() != null && gatewayImage.getId() != 0) {
			gatewayImage.setModifyby(login.getUsername());
			gatewayImage.setModifydate(DateUtils.getCurrentDate());
			gatewayImage.setModifystamp(DateUtils.getCurrentDate());
			novaGatewayImageService.update(gatewayImage);
		} else {
			gatewayImage.setAddby(login.getUsername());
			gatewayImage.setAdddate(DateUtils.getCurrentDate());
			novaGatewayImageService.insert(gatewayImage);
		}

		return gatewayImageList(request, session, 1, new GatewayImage(),
				modelMap);

	}

	@RequestMapping(value = "deleteGatewayImage.shtml")
	public ModelAndView deletecollectArea(Integer gatewayImageId,
			HttpServletRequest request, HttpSession session, ModelMap modelMap) {
		novaGatewayImageService.delete(gatewayImageId);
		return gatewayImageList(request, session, 1, new GatewayImage(),
				modelMap);
	}

}
